home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / msg / receive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  3.7 KB  |  157 lines

  1. /*
  2.  *   $RCSfile: receive.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:54 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "object.h"
  48. #include "msgdefs.h"
  49. #include "thread.h"
  50. #include "semaphore.h"
  51. #include "link.h"
  52. #include "host.h"
  53. #include "msgvector.h"
  54. #include "threadstate.h"
  55. #include "bitvec.h"
  56. #include "msg_funcs.h"
  57. #include "thread_funcs.h"
  58.  
  59.  int
  60. receive (
  61.  
  62.     register LINK        *link,
  63.     register char        *buffer,
  64.     register int        readBytes 
  65. )
  66. {
  67.  
  68.     register int        bytesRead;
  69.     register int        remainBytes;
  70.     register int        totalBytes;
  71.  
  72.  
  73.     TRPRINT(TR_MSG, TR_LEVEL_1, ("reading link:%d readBytes:%d", link->id, readBytes));
  74.  
  75. #ifdef DEBUG
  76.     /*
  77.      *    Make sure that if we're not reading in a message header
  78.      *  then the buffer is not in the thread. 
  79.      */
  80.     if (readBytes > sizeof(MESSAGE)) {
  81.         SM_ASSERT(LEVEL_1, buffer != (char*)&(Active->message));
  82.     }
  83. #endif
  84.  
  85.     /*
  86.      *    read the messages, repeatedly perhaps
  87.      */
  88.     for (totalBytes = 0; totalBytes < readBytes; totalBytes += bytesRead)    {
  89.  
  90.         /*
  91.          *    calculate remaining bytes
  92.          */
  93.         remainBytes = readBytes - totalBytes;
  94.         SM_ASSERT(LEVEL_3, remainBytes > 0);
  95.         TRPRINT(TR_MSG, TR_LEVEL_2, ("want to read:%d", remainBytes));
  96.  
  97.         /*
  98.          *    read the remaining bytes
  99.          */
  100.         bytesRead = read(link->id, buffer + totalBytes, remainBytes);
  101.         TRPRINT(TR_DISK, TR_LEVEL_2, ("bytesRead:%d", bytesRead));
  102.  
  103.         /*
  104.          *    check to see we read more data than we want
  105.          */
  106.         SM_ASSERT(LEVEL_3, bytesRead <= remainBytes);
  107.  
  108.         /*
  109.          *    check to see if was an error or a close
  110.          */
  111.         if (bytesRead < 0)    {
  112.  
  113.             SM_ERROR(TYPE_SYS, errno);
  114.         }
  115.  
  116.         /*
  117.          *    check to see if eof
  118.          */
  119.         if ((bytesRead == 0) || (bytesRead < 0))    {
  120.  
  121.             /*
  122.              *    shut down the link
  123.              */
  124.             switch (link->linkClass)    {
  125.  
  126.                 case CL_DGRAM:
  127.                 case CL_CONNECT:
  128.                 case CL_DISK:
  129.  
  130.                     SM_ERROR(TYPE_CRASH, esmINTERNAL);
  131.                     break;
  132.  
  133.                 case CL_CLIENT:
  134.  
  135.                     freeClientLink(link);
  136.                     return(esmFAILURE);
  137.                     break;
  138.  
  139.                 default:
  140.  
  141.                     SM_ERROR(TYPE_FATAL, esmINTERNAL);
  142.                     break;
  143.             }
  144.  
  145.         } else if (bytesRead < remainBytes)    {
  146.  
  147.             /*
  148.              *    put ourselves on the read queue for the link
  149.              */
  150.             waitList( &(link->tcbList), THREAD_RECEIVE_WAIT );
  151.  
  152.         } 
  153.     }
  154.     incPages(readBytes, RECEIVED, FALSE); /* MsgStats */
  155.     return esmNOERROR;
  156. }
  157.